package com.sihai.core.web;


import com.sihai.core.beans.BeanLocator;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;


/**
 * <code>SpringContextLoaderListener</code> 是一个ServletContextListener,
 * 扩展了spring提供的ContextLoaderListener，使servlet应用服务器启动的时候初始化BeanDiscoverer
 */
public class SpringContextLoaderListener extends ContextLoaderListener {

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        super.contextDestroyed(event);
        BeanLocator.setApplicationContext(null);
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        super.contextInitialized(event);
        BeanLocator.setApplicationContext(
                WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()));
    }

}
